package com.qqls.youxiangousys.pj.sys.controller;

import com.qqls.youxiangousys.pj.common.entity.JsonResult;
import com.qqls.youxiangousys.pj.common.entity.Pagination;
import com.qqls.youxiangousys.pj.sys.entity.SysNotice;
import com.qqls.youxiangousys.pj.sys.service.SysNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("notice")
public class SysNoticeController {
    @Autowired
    private SysNoticeService nService;

    /**
     * 按内容查所有
     * @param noticeContent
     * @param curPage
     * @param pageSize
     * @return
     */
    @RequestMapping("findAll")
    public JsonResult findAll(String noticeContent,Integer curPage,Integer pageSize){
        Pagination pageObj = nService.findAll(noticeContent,curPage,pageSize);
        return new JsonResult(pageObj);
    }


     //添加内容
    @RequestMapping("addNotice")
    public JsonResult addNotice(String noticeContent,String noticeTimeStr,String createdTimeStr) throws ParseException {
        String str = "yyyy-MM-dd";
        SimpleDateFormat sdf = new SimpleDateFormat(str);
        Date noticeTime = sdf.parse(noticeTimeStr);
        Date createdTime = sdf.parse(createdTimeStr);
        SysNotice sysNotice = new SysNotice();
        sysNotice.setNoticeTime(noticeTime);
        sysNotice.setNoticeContent(noticeContent);
        sysNotice.setCreatedTime(createdTime);
        System.out.println(sysNotice.toString());
        JsonResult jr=new JsonResult(nService.addNotice(sysNotice));
        jr.setMessage("添加成功");
        return jr;
    }
    //修改
    @RequestMapping("updateNotice")
    public JsonResult findNoticeById(SysNotice notice){
        JsonResult jr=new JsonResult(nService.updateNotice(notice));
        jr.setMessage("修改成功");
        return jr;
    }
    //状态
    @RequestMapping("updateValId")
    public JsonResult updateValId(Integer id,Integer noticeState){
        JsonResult jr=new JsonResult(nService.updateValId(id,noticeState));
        jr.setMessage("修改成功");
        return jr;
    }
    @RequestMapping("deleteNotice")
    public JsonResult deleteNotice(@RequestParam(required = false,value = "ids[]")Integer[] ids){
        return new JsonResult(nService.deleteNotice(ids));
    }
}
